package basic.basic_class04;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 查看一个树是否为搜索二叉树（中序遍历结果是有序的树称为搜索二叉树）
 */
public class Code_07_IsBSTAndCBT {

    // 节点类
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    /**
     * 使用非递归的方式判断一个树是否为搜索二叉树
     * @param head
     */
    public static boolean isBST(Node head) {
        Stack<Node> stack = new Stack<Node>();
        int cur = Integer.MIN_VALUE;
        while(!stack.isEmpty() || head != null){
            if(head != null){
                stack.push(head);
                head = head.left;
            }else{
                head = stack.pop();
                if(head.value < cur) {
                    return false;
                }
                cur = head.value;
                head = head.right;
            }
        }
        return true;
    }

    /**
     * 检查一个树是否为完全二叉树(按层进行遍历，如果碰到了条件执行)
     * @param head
     * @return
     */
    public static boolean isCBT(Node head) {
        if(head == null) return true;
        boolean flag = false;    // 标记是否开启叶子节点模式
        Queue<Node> queue = new LinkedList<Node>();
        queue.add(head);
        Node l = null;
        Node r = null;
        while(!queue.isEmpty()){
            head = queue.poll();
            l = head.left;
            r = head.right;
            if((flag && (l != null || r != null)) || (l == null && r != null)){
                return false;
            }
            if(l != null){
                queue.add(l);
            }
            if(r != null){
                queue.add(r);
            }
            if(l == null || r == null){
                //只要右一个为空，则开启叶子节点模式
                flag = true;
            }
        }
        return true;
    }

    // for test -- print tree
    public static void printTree(Node head) {
        System.out.println("Binary Tree:");
        printInOrder(head, 0, "H", 17);
        System.out.println();
    }

    public static void printInOrder(Node head, int height, String to, int len) {
        if (head == null) {
            return;
        }
        printInOrder(head.right, height + 1, "v", len);
        String val = to + head.value + to;
        int lenM = val.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenM - lenL;
        val = getSpace(lenL) + val + getSpace(lenR);
        System.out.println(getSpace(height * len) + val);
        printInOrder(head.left, height + 1, "^", len);
    }

    public static String getSpace(int num) {
        String space = " ";
        StringBuffer buf = new StringBuffer("");
        for (int i = 0; i < num; i++) {
            buf.append(space);
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        Node head = new Node(4);
        head.left = new Node(2);
        head.right = new Node(6);
        head.left.left = new Node(1);
        //head.left.right = new Node(3);
        head.right.left = new Node(5);

        printTree(head);
        //System.out.println(isBST(head));
        System.out.println(isCBT(head));

    }
}
